<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 用户头像 -->
      <div class="face_box">
        <img src="@/assets/logo.png" />
      </div>
      <!-- 表单区域 -->
      <el-form class="LoginForm" :model="login_form" :rules="rules" ref="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="login_form.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" v-model="login_form.password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="ctrl_btns">
          <el-button type="primary" @click="submitForm('login_form')">登录</el-button>
          <el-button type="info" @click="resetForm('login_form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      login_form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return false
        // 发送请求
        const { data: res } = await this.$http.post('login', this.login_form)
        console.log(res)
        if (res.meta.status === 200) {
          window.sessionStorage.setItem('token', res.data.token)
          this.$message({
            message: '登陆成功！',
            type: 'success'
          })
          this.$router.push('/home')
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;

    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;

    box-shadow: 0 0 5px 1px rgba(255, 255, 255, 0.3);

    .face_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 5px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      box-shadow: 0 0 5px 1px rgba(255, 255, 255, 0.3);
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .LoginForm {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;
      .ctrl_btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
